﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Win32;
using System.IO;

namespace AtletIS.Shared.Helpers
{
    public static class ConnectionHelper
    {
        /// <summary>
        ///  Nastaví ConnectionStringy pre všetky aplikácie
        /// </summary>
        public static bool SetConnectionStrings(string computer, string server, string db)
        {
            try
            {
                if (computer != string.Empty)
                    server = computer + "\\" + server;

                string txt = string.Empty;
                string txtSync = string.Empty;
                
                txt += "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + Environment.NewLine;
                txt += "<configuration>" + Environment.NewLine;
                
                txt += "  <configSections>" + Environment.NewLine;
                txt += "    <sectionGroup name=\"userSettings\" type=\"System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" >" + Environment.NewLine;
                txt += "      <section name=\"AtletIS.Shared.Properties.Settings\" type=\"System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" allowExeDefinition=\"MachineToLocalUser\" requirePermission=\"false\" />" + Environment.NewLine;
                txt += "    </sectionGroup>";
                txt += "  </configSections>" + Environment.NewLine;
                
                txt += "  <connectionStrings>" + Environment.NewLine;
                txt += "    <add name=\"AtletIS.Shared.Properties.Settings.atletisConnectionString\"" + Environment.NewLine;
                txt += "      connectionString=\"Server=" + server + "; Database=" + db + "; User Id=atletis; Password=atletis;\"" + Environment.NewLine;
                txt += "      providerName=\"System.Data.SqlClient\" />" + Environment.NewLine;
                txt += "  </connectionStrings>" + Environment.NewLine;
                
                txt += "  <userSettings>" + Environment.NewLine;
                txt += "    <AtletIS.Shared.Properties.Settings>" + Environment.NewLine;
                txt += "      <setting name=\"BoardPort\" serializeAs=\"String\">" + Environment.NewLine;
                txt += "        <value>COM1</value>" + Environment.NewLine;
                txt += "      </setting>" + Environment.NewLine;
                txt += "    </AtletIS.Shared.Properties.Settings>" + Environment.NewLine;
                txt += "  </userSettings>" + Environment.NewLine;

                txtSync = txt;
                txtSync += "  <system.serviceModel>" + Environment.NewLine;
                txtSync += "    <bindings>" + Environment.NewLine;
                txtSync += "      <basicHttpBinding>" + Environment.NewLine;
                txtSync += "        <binding name=\"BasicHttpBinding_IAtletisService\" maxReceivedMessageSize=\"10485760\" />" + Environment.NewLine;
                txtSync += "      </basicHttpBinding>" + Environment.NewLine;
                txtSync += "    </bindings>" + Environment.NewLine;
                txtSync += "    <client>" + Environment.NewLine;
                txtSync += "      <endpoint address=\"http://atletis.sk/AtletisService.svc\" binding=\"basicHttpBinding\"" + Environment.NewLine;
                txtSync += "        bindingConfiguration=\"BasicHttpBinding_IAtletisService\" contract=\"AtletisService.IAtletisService\"" + Environment.NewLine;
                txtSync += "        name=\"BasicHttpBinding_IAtletisService\" />" + Environment.NewLine;
                txtSync += "    </client>" + Environment.NewLine;
                txtSync += "  </system.serviceModel>" + Environment.NewLine;

                txt += "</configuration>" + Environment.NewLine;
                txtSync += "</configuration>" + Environment.NewLine;

                WriteFile("AtletIS.exe.config", txt);
                WriteFile("Speaker.exe.config", txt);
                WriteFile("Sector.exe.config", txt);
                WriteFile("Track.exe.config", txt);
                WriteFile("Windspeed.exe.config", txt);
                WriteFile("OnlineManager.exe.config", txt);
                WriteFile("ChipViewer.exe.config", txt);
                WriteFile("Press.exe.config", txt);
                WriteFile("BoardControl.exe.config", txt);
                WriteFile("Sync.exe.config", txtSync);

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Connections", 
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;              
            }
        }

        /// <summary>
        ///  Zapíše [txt] do súboru [path]
        /// </summary>
        private static void WriteFile(string path, string txt)
        {
            if (File.Exists(path))
            {
                File.SetAttributes(path, FileAttributes.Normal);
            }
                        
            StreamWriter sw = new StreamWriter(path, false, Encoding.Default);
            sw.Write(txt);

            sw.Close();
            sw.Dispose();

            File.SetAttributes(path, FileAttributes.ReadOnly);
        }
        




    }
}
